<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2022/8/9
  Time: 12:45
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
  <head>
    <base href="${pageContext.request.contextPath}/">
    <title>$Title$</title>
    <style>
      table tr:nth-child(even){
        background: lightgray;
      }
    </style>
    <script src="jq/jquery.js"></script>
  </head>
  <body>
    <h1 align="center">元宇宙商品列表</h1>
    <table align="center" border="1px">
      <tr style="background: darkgray">
        <th>编号</th>
        <th>名称</th>
        <th>描述</th>
        <th>价格</th>
        <th>状态</th>
        <th>操作</th>
      </tr>
      <c:forEach items="${sessionScope.list}" var="mp">
        <tr>
          <td>${mp.id}</td>
          <td>${mp.product_name}</td>
          <td>${mp.product_desc}</td>
          <td>${mp.product_price}</td>
          <c:if test="${mp.flag==0}">
            <td>下架</td>
          </c:if>
          <c:if test="${mp.flag==1}">
            <td>上架</td>
          </c:if>
          <td><a href="javascript:void(0)" onclick="update('${mp.id}','${mp.flag}')">修改</a><a href="javascript:void(0)" onclick="remove('${mp.id}')">删除</a></td>
        </tr>
      </c:forEach>
    </table>
    <div align="center"><a href="add.jsp">新增元宇宙商品</a></div>
  </body>
  <script>
    function update(id,flag) {
      if(confirm("确认修改商品状态吗？")) {
        $.getJSON(
                "MetaverseProductUpdate",
                {id: id, flag: flag},
                function (data) {
                  if (data) {
                    alert("修改成功");
                    location.href = "MetaverseProductList"
                  } else {
                    alert("修改失败")
                  }
                }
        )
      }
    }
    function remove(id) {
      if(confirm("确认是否删除？")) {
        $.getJSON(
                "MetaverseProductDelete",
                {id: id},
                function (data) {
                  if (data) {
                    alert("删除成功");
                    location.href = "MetaverseProductList"
                  } else {
                    alert("删除失败")
                  }
                }
        )
      }
    }
  </script>
</html>